// ****************************************************************************
// *     Filename:  EventAction.cc
// *  Description:  
// *      License:  Geant4 Software License 
// *                http://geant4.cern.ch/license/LICENSE.html
// *       Author:  Tran Hoai Nam (nam), nam@kuno-g.phys.sci.osaka-u.ac.jp
// * Organization:  Osaka University
// *      Created:  2012-09-14 15:31:31
// ****************************************************************************

#include "G4Event.hh"
#include "G4HCofThisEvent.hh"
#include "G4SDManager.hh"

#include "EventAction.hh"
#include "MyHit.hh"
#include "PrimaryGeneratorAction.hh"
#include <string>

EventAction::EventAction() 
{
	fpSDMan  = G4SDManager::GetSDMpointer();
}

EventAction::EventAction(PrimaryGeneratorAction *priGenAction) 
{
	fpPriGen = priGenAction;
	fpSDMan  = G4SDManager::GetSDMpointer();
}

EventAction::~EventAction() {;}

void EventAction::BeginOfEventAction(const G4Event* event) 
{
	G4cout<<"hello\n";
}

void EventAction::EndOfEventAction(const G4Event* event)
{
	ReadoutDetector(event,"aSD");
	G4cout<<"endl!"<<G4endl;
}

void EventAction::ReadoutDetector(const G4Event *event, G4String detName) 
{
	G4int hitsCollID = fpSDMan->GetCollectionID(detName);
	if(hitsCollID >= 0)
	{
		G4HCofThisEvent *aHCE = event->GetHCofThisEvent();
		MyHitsCollection* hitsCollection= 
			dynamic_cast<MyHitsCollection*>(aHCE->GetHC(hitsCollID));
		G4int NoOfHits = hitsCollection->GetSize();
		G4cout<<"Detetector: "<<detName<<", # of hits: "<<NoOfHits<<G4endl;
		//Scan all hits
		for (int i=0;i<NoOfHits;i++)
		{
			MyHit* aHit = (*hitsCollection)[i];
				aHit->Print();
		}
	}
}
